<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <!-- <el-form-item>
        <el-input v-model="dataForm.userName"
                  placeholder="用户名"
                  clearable></el-input>
      </el-form-item>-->
      <el-form-item>
        <!-- <el-button @click="getDataList()">查询</el-button> -->
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!-- <el-button type="danger"
                   @click="deleteHandle()"
        :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
      <!-- <el-table-column type="selection"
                       header-align="center"
                       align="center"
                       width="50">
      </el-table-column>-->
      <el-table-column prop="number" header-align="center" align="center" width="240" label="代号"/>
      <el-table-column prop="username" header-align="center" align="center" label="用户名"/>
      <el-table-column prop="password" header-align="center" align="center" label="密码"/>
      <el-table-column prop="role" header-align="center" align="center" label="角色">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.role == 0" size="small" type="danger">管理员</el-tag>
          <el-tag v-else-if="scope.row.role == 1" size="small" type="warning">老师</el-tag>
          <el-tag v-else size="small">学生</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注" />
      <!-- <el-table-column prop="email"
                       header-align="center"
                       align="center"
                       label="邮箱">
      </el-table-column>
      <el-table-column prop="mobile"
                       header-align="center"
                       align="center"
                       label="手机号">
      </el-table-column>-->
      <!-- <el-table-column prop="status"
                       header-align="center"
                       align="center"
                       label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0"
                  size="small"
                  type="danger">禁用</el-tag>
          <el-tag v-else
                  size="small">正常</el-tag>
        </template>
      </el-table-column>-->
      <!-- <el-table-column prop="createTime"
                       header-align="center"
                       align="center"
                       width="180"
                       label="创建时间">
      </el-table-column>-->
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.role==0"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row)"
          >修改</el-button>
          <el-button
            :disabled="scope.row.role==0"
            type="text"
            size="small"
            @click="deleteHandle(scope.row._id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
import AddOrUpdate from './user-add-or-update'
import { getUsers, delUsers } from '@/api/manage'
export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      dataForm: {
        userName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let data={
            'pageNum': this.pageIndex,
            'pageSize': this.pageSize,
          }
      getUsers(data)
        // this.$http({
        //   url: this.$http.adornUrl('/sys/user/list'),
        //   method: 'get',
        //   params: this.$http.adornParams({
        //     'page': this.pageIndex,
        //     'limit': this.pageSize,
        //     'username': this.dataForm.userName
        //   })
        // })
        .then(res => {
          if (res.code === 1) {
            const data = res.data
            this.dataList = data.userLists
            this.totalPage = data.totalPage
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row)
      })
    },
    // 删除
    deleteHandle(id) {
      var userId = id
      // ? [id]
      // : this.dataListSelections.map(item => {
      //     return item.userId;
      //   });
      this.$confirm(`确定对id=${id}进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delUsers({ userId }).then(res => {
            if (res.code === 1) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              const data = res.data
              this.$message.error(data.msg)
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="scss">
.mod-user {
  padding: 20px;
}
</style>

